package com.ce.pms.renovation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.renovation.dao.ICheckConfirmItemDao;
import com.ce.pms.renovation.dao.IRenovateRequisitionDao;
import com.ce.pms.renovation.model.CheckConfirmItemDO;
import com.ce.pms.renovation.model.RenovateRequisitionDO;
import com.ce.pms.renovation.service.ICheckConfirmService;
import com.ce.pms.renovation.vo.CheckConfirmItemVO;
import com.ce.pms.system.renovation.dao.ICheckConfirmItemConfigDao;
import com.ce.pms.system.renovation.model.CheckConfirmItemConfigDO;
import com.ce.util.BeanUtil;

/**
 * 功能描述：进场确认服务类<br>
 * @author 罗军林 创建日期：2008-12-26
 */
public class CheckConfirmServiceImpl extends PmsBaseServiceImpl implements ICheckConfirmService{
	private ICheckConfirmItemConfigDao checkConfirmItemConfigDao;
	private ICheckConfirmItemDao checkConfirmItemDao;
	private IRenovateRequisitionDao renovateRequisitionDao;

   /**
    * 功能描述:进场确认，如果是第一次将是添加，否则则修改<br>
    * @param requisitionId
    * @param checkConfirmItemVOs<br>
    * Create author:罗军林<br>
    * Create on:2008-12-26<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public void checkConfirmItems(Long requisitionId,List<CheckConfirmItemVO> checkConfirmItemVOs){
	   RenovateRequisitionDO requisitionDo=renovateRequisitionDao.findById(requisitionId);
	   if( SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(requisitionDo.getCheckConfirmYn()) ){
		   editCheckConfirmItem(checkConfirmItemVOs, requisitionDo);
	   }else{
		  isAllowEditConfirmItem(requisitionDo);
		  addCheckConfirmItems(checkConfirmItemVOs, requisitionDo);
		  requisitionDo.setCheckConfirmYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
	   }
   }

   /**
    * 功能描述:查询所有的确认项目<br>
    * 如果确认过，则查该装修申请的确认项目，否则从系统配置中查找<br>
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-26<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public List<CheckConfirmItemVO> getAllCheckConfirmItemConfigs(Long requistionId){
	    List<CheckConfirmItemDO> checkConfirmList= checkConfirmItemDao.getAllCheckConfirmItemConfigs(requistionId);
	    List<CheckConfirmItemVO> tempList=new ArrayList<CheckConfirmItemVO>();
	    if(null==checkConfirmList || checkConfirmList.size()==0){
	    	List<CheckConfirmItemConfigDO> itemConfigList=checkConfirmItemConfigDao.queryAll();
	    	for(CheckConfirmItemConfigDO item : itemConfigList){
	    		CheckConfirmItemVO itemVO=new CheckConfirmItemVO();
    			itemVO.setItemConfig(item.getItemName());
    			itemVO.setItemConfigId(item.getKeyword());
    			tempList.add(itemVO);
	    	}
	    }else{
		    for(CheckConfirmItemDO item : checkConfirmList){
		    	CheckConfirmItemVO itemVO=new CheckConfirmItemVO();
		    	BeanUtil.convertVoAsDo(item, itemVO);
		    	itemVO.setItemConfigId(item.getItemConfig().getKeyword());
		    	itemVO.setItemConfig(item.getItemConfig().getItemName());
		    	tempList.add(itemVO);
		    }
	    }
	    return tempList;
   }


	/**
	 * 功能描述：批量添加确认项目<br>
	 * @param checkConfirmItemVOs
	 * @param requisitionDo<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-26<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void addCheckConfirmItems(List<CheckConfirmItemVO> checkConfirmItemVOs, RenovateRequisitionDO requisitionDo) {
		for(CheckConfirmItemVO item : checkConfirmItemVOs){
			  CheckConfirmItemDO checkConfirmItemDO=new CheckConfirmItemDO();
			  assembleCheckConfirmItemV2D(item,checkConfirmItemDO);
			  checkConfirmItemDO.setRenovateRequisition(requisitionDo);
			  checkConfirmItemDao.saveObject(checkConfirmItemDO);
		  }
	}

   /**
    * 功能描述:<br>
    * @param checkConfirmItemVO
    * @param checkConfirmItemDO<br>
    * Create author:罗军林<br>
    * Create on:2008-12-26<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   private void assembleCheckConfirmItemV2D(CheckConfirmItemVO checkConfirmItemVO, CheckConfirmItemDO checkConfirmItemDO)
   {
		  BeanUtil.convertVoAsDo(checkConfirmItemVO, checkConfirmItemDO);
		  checkConfirmItemDO.setItemConfig( checkConfirmItemConfigDao.loadById(checkConfirmItemVO.getItemConfigId()));
   }

	/**
	 * 功能描述:批量修改确认项目<br>
	 * @param checkConfirmItemVOs
	 * @param requisitionDo<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-26<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void editCheckConfirmItem(List<CheckConfirmItemVO> checkConfirmItemVOs, RenovateRequisitionDO requisitionDo) {
		for(CheckConfirmItemVO item : checkConfirmItemVOs){
				CheckConfirmItemDO checkConfirmItemDO=new CheckConfirmItemDO();
				assembleCheckConfirmItemV2D(item,checkConfirmItemDO);
				checkConfirmItemDO.setRenovateRequisition(requisitionDo);
				checkConfirmItemDao.saveOrUpdateObject(checkConfirmItemDO);
		   }
	}

	/**
	 * 功能描述:是否允许修改进场确认项目<br>
	 * @param renovateRequisitionId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-26<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
   private Boolean isAllowEditConfirmItem(RenovateRequisitionDO requisitionDo){
	   if( !SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_PASS.equals(requisitionDo.getRequisitionState())){
		   throw new BusinessException("renovate:renovate_requistion_pass_checkconfirm",BusinessException.ERROR);
	   }
    return true;
   }


   public void setCheckConfirmItemConfigDao(ICheckConfirmItemConfigDao checkConfirmItemConfigDao) {
		this.checkConfirmItemConfigDao = checkConfirmItemConfigDao;
	}

	public void setCheckConfirmItemDao(ICheckConfirmItemDao checkConfirmItemDao) {
		this.checkConfirmItemDao = checkConfirmItemDao;
	}

	public void setRenovateRequisitionDao(IRenovateRequisitionDao renovateRequisitionDao) {
		this.renovateRequisitionDao = renovateRequisitionDao;
	}
}
